package com.sys.servlet;


import com.sys.entity.LiveCommerce;
import com.sys.service.LiveCommerceService;
import com.sys.service.impl.LiveCommerceServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDate;


@WebServlet(urlPatterns = "/add")
public class LiveCommerceAddHttpServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        response.setContentType("text/html");

        String carName = request.getParameter("streamer");
        String cityName = request.getParameter("livePlatform");
        String userName = request.getParameter("product");
        String status = request.getParameter("status");
        double price = Double.parseDouble(request.getParameter("price"));
        LocalDate buyTime = LocalDate.parse(request.getParameter("liveTime"));
        int status1 = 0;
        if(status.equals("正常直播")){
            status1 = 1;
        }

        LiveCommerce powerBattery = new LiveCommerce();

        powerBattery.setStreamer(carName);
        powerBattery.setLivePlatform(cityName);
        powerBattery.setProduct(userName);
        powerBattery.setLiveTime(buyTime);
        powerBattery.setPrice(price);
        powerBattery.setStatus(status1);

        LiveCommerceService f = new LiveCommerceServiceImpl();
        int insert = f.insert(powerBattery);
        if(insert>0){
            out.print("<script>alert('添加成功');location.href='list'</script>");
        }else{
            out.print("<script>alert('添加失败');location.reload()</script>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
